<?php
namespace app\common\model;
class OrderClient extends Common {
	protected $insert = ['addtime' => SYS_TIME, 'product'];
	protected $update = ['product'];
	// // 追加属性
	protected $append = [
		'carthumb', 'cartypethumb',
	];
	protected function getCartypethumbAttr($value, $data) {
		$thumb = model('Cartype')->where('id', $data['carid'])->value('thumb');
		return $thumb;
	}
	protected function getNicknameAttr($value, $data) {
		$info = model("Member")->get_data(['item_id' => $data['userid']]);
		return $info['nickname'];
	}
	protected function getAlldataAttr($value, $data) {
		$kind = config('CLIENT_KIND');
		$paytype = config('PAYTYPE');
		$status = [-1 => '取消订单', 0 => '未支付', 1 => '支付成功', 5 => '预付款'];
		$users = model('AdminUser')->where('userid', 'in', [$data['userid'], $data['brokerid'], $data['dealerid']])->order('userid desc')->column('userid, realname');
		$data['dealer'] = isset($users[$data['dealerid']]) ? $users[$data['dealerid']] : '--';
		$data['zjname'] = isset($users[$data['userid']]) ? $users[$data['userid']] : '--';
		$data['name'] = model('Client')->where('id', $data['clientid'])->value('name');
		$data['kindname'] = $data['kind'] && isset($kind[$data['kind']]) ? $kind[$data['kind']] : '';
		$data['payname'] = $data['paytype'] && isset($paytype[$data['paytype']]) ? $paytype[$data['paytype']] : '';
		$data['realname'] = isset($users[$data['brokerid']]) ? $users[$data['brokerid']] : '--';
		$data['statusname'] = isset($status[$data['status']]) ? $status[$data['status']] : '未支付';
		$data['paydate'] = $data['paytime'] ? date('Y-m-d H:i:s', $data['paytime']) : '未支付';
		return $data;
	}
	protected function getNameAttr($value, $data) {
		return model('Client')->where('id', $data['clientid'])->value('name');
	}
	protected function getAddRealnameAttr($value, $data) {
		return model('AdminUser')->where('userid', $data['adduserid'])->value('realname');
	}
	protected function getPaynameAttr($value, $data) {
		return config('PAYTYPE.' . $data['paytype']);
	}
	protected function getStatusnameAttr($value, $data) {
		return config('PAY_STATUS.' . $data['status']);
	}
	protected function getPaytimeAttr($value, $data) {
		return $value ? date('Y-m-d H:i:s', $value) : '未支付';
	}
	protected function getDatelineAttr($value, $data) {
		return date('Y-m-d H:i:s', $value);
	}
	protected function getUinfoAttr($value, $data) {
		return model('AdminUser')->where('userid', $data['brokerid'])->field('realname, phone, username, avatar')->find();
	}
	protected function getCarThumbAttr($value, $data) {
		return model('Car')->where('id', $data['carid'])->value('thumb');
	}
	protected function getAcceAttr($value, $data) {
		$acce = $data['acce'] ? json_decode($data['acce'], true) : '';
		return model("Accessory")->get_fileurl($acce);
	}
	protected function setProductAttr($value, $data) {
		if (isset($data['carid']) && $data['carid']) {
			$fullname = model('Cartype')->where('id', $data['cartypeid3'])->value('fullname');
			$carname = model('Car')->where('id', $data['carid'])->value('title');
			return $fullname . ' ' . $carname;
		}
		if (isset($data['cartypeid3']) && $data['cartypeid3']) {
			return model('Cartype')->where('id', $data['cartypeid3'])->value('fullname');
		}
		if (isset($data['cartypeid2']) && $data['cartypeid2']) {
			return model('Cartype')->where('id', $data['cartypeid2'])->value('title');
		}
		if (isset($data['cartypeid1'])) {
			return model('Cartype')->where('id', $data['cartypeid1'])->value('title');
		}
		return '';
	}
	public function save_datas($data, $userid = 0, $dealerid = 0) {
		if (!isset($data['nums']) || !$data['nums']) {
			$this->setError('请填写购买数量');
			return false;
		}
		if (!isset($data['totalprice']) || !$data['totalprice']) {
			$this->setError('请填写成交总额');
			return false;
		}
		if (!isset($data['paytype']) || !$data['paytype']) {
			$this->setError('请选择付款方式');
			return false;
		}
		if (isset($data['acce']) && is_array($data['acce'])) {
			$data['acce'] = json_encode($data['acce']);
		}
		$data['userid'] = $userid;
		$data['dealerid'] = $dealerid;
		$data['ordersn'] = 'client' . date('YmdHis', SYS_TIME) . rand(10000, 99999);
		$id = $this->save_data($data);
		if (!empty($data['thumbs'])) {
			foreach ($data['thumbs'] as $v) {
				$acces = [
					'fileurl' => $v,
					'typeid' => 6,
					'contentid' => $id,
				];
				model('Accessory')->save_data($acces);
			}
		}
		if ($data['status'] == 1) {
			exec_php('/index/cron/send_coupons', ['userid' => $data['brokerid'], 'type' => $data['kind'], 'ocid' => $id]);
		}
		return true;
		// if ($data['status'] == 1) {
		//     //经纪人记录
		//     model('Statistics')->add_log($data['brokerid'], 1, 0, $data['totalprice'], 1);
		//     //销售总监记录
		//     model('Statistics')->add_log($data['userid'], 2, 0, $data['totalprice'], 1);
		//     // 经销商记录
		//     $dealerid = model('AdminUser')->where('userid', $data['userid'])->value('puserid');
		//     model('Statistics')->add_log($dealerid, 3, 0, $data['totalprice'], 1);
		//     // 区域记录
		//     $quyuid = model('AdminUser')->where('userid', $dealerid)->value('puserid');
		//     model('Statistics')->add_log($quyuid, 4, 0, $data['totalprice'], 1);
		// }
		//不能用model 会重复操作
		// return model('Client')->where('id', $data['clientid'])->update(['typeid'=>$data['typeid'], 'skilled'=>$data['skilled']]);
	}
	public function update_datas($data, $userid = 0) {
		if (!empty($data['thumbs'])) {
			foreach ($data['thumbs'] as $v) {
				$acces = [
					'fileurl' => $v,
					'typeid' => 6,
					'contentid' => $id,
				];
				model('Accessory')->save_data($acces);
			}
		}
		if (isset($data['acce']) && is_array($data['acce'])) {
			$data['acce'] = json_encode($data['acce']);
		}
		$id = $this->update_data($data);
		if ($data['status'] == 1) {
			exec_php('/index/cron/send_coupons', ['userid' => $data['brokerid'], 'type' => $data['kind'], 'ocid' => $id]);
		}
		return true;
		// $info = $this->where('id', $data['id'])->find();
		// if ($data['status'] == 1 && $info['status'] != 1) {
		//     //经纪人记录
		//     model('Statistics')->add_log($data['brokerid'], 1, 0, $data['totalprice'], 1);
		//     //销售总监记录
		//     model('Statistics')->add_log($data['userid'], 2, 0, $data['totalprice'], 1);
		//     // 经销商记录
		//     $dealerid = model('AdminUser')->where('userid', $data['userid'])->value('puserid');
		//     model('Statistics')->add_log($dealerid, 3, 0, $data['totalprice'], 1);
		//     // 区域记录
		//     $quyuid = model('AdminUser')->where('userid', $dealerid)->value('puserid');
		//     model('Statistics')->add_log($quyuid, 4, 0, $data['totalprice'], 1);
		//     $data['paytime'] = SYS_TIME;
		// }
	}
}